﻿@model NCP.Model.GroupPurchase
@{
    ViewData["Title"] = "集采批次维护";
}
<div class="layui-fluid">
    <form class="layui-card layui-form" lay-filter="layuiadmin-form-list" id="dtFrm" style="padding: 20px 30px 0 0;">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label for="DictType" class="layui-form-label">
                    <span class="x-red">*</span>编码
                </label>
                <div class="layui-input-block">
                    <input type="text" id="Code" name="Code" @Html.Raw(ViewData["type"].ToString() == "edit" ? "readonly" : "") value="@Model.Code" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="Name" class="layui-form-label">
                    <span class="x-red">*</span>名称
                </label>
                <div class="layui-input-block">
                    <input type="text" id="Name" name="Name" @Html.Raw(Model.Enabled == "1" ? "disabled" : "") value="@Model.Name" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="Code" class="layui-form-label">
                    <span class="x-red">*</span>年份
                </label>
                <div class="layui-input-block">
                    <input type="text" id="Year" name="Year" @Html.Raw(Model.Enabled == "1" ? "disabled" : "") value="@Model.Year" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="Code" class="layui-form-label">
                    <span class="x-red">*</span>开始日期
                </label>
                <div class="layui-input-block">
                    <input type="text" id="BeginDate" name="BeginDate" @Html.Raw(Model.Enabled == "1" ? "disabled" : "") value="@Model.BeginDate.ToString("yyyy-MM-dd")" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="Code" class="layui-form-label">
                    <span class="x-red">*</span>结束日期
                </label>
                <div class="layui-input-block">
                    <input type="text" id="EndDate" name="EndDate" @Html.Raw(Model.Enabled == "1" ? "disabled" : "") value="@Model.EndDate.ToString("yyyy-MM-dd")" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div id="btnContainer" class="layui-form-item" style="text-align:center;padding-top:50px;">
                @if (Model.Enabled != "1")
                {
                    if (ViewData["type"].ToString() == "edit")
                    {
                        <div style="position:absolute; left:30px;">
                            <button class="layui-btn layui-btn-normal layui-btn-radius" type="button" onclick="doEnabled()">
                                启用集采批次
                            </button>
                        </div>
                    }
                    <button class="layui-btn" type="button" onclick="doSave()">
                        保存
                    </button>
                }
                <button class="layui-btn layui-btn-danger" type="button" onclick="onCancel()">
                    关闭
                </button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    var layer;
    var admin;
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layer', 'index', 'form', 'laydate'], function () {
        var layer = layui.layer, $ = layui.$, form = layui.form;
        var laydate = layui.laydate;
        admin = layui.admin;

        laydate.render({
            elem: "#Year"
            , type: "year"
        });
        laydate.render({
            elem: "#BeginDate"
        });
        laydate.render({
            elem: "#EndDate"
        });
    });

    function doSave() {
        if (valid("#dtFrm") > 0) {
            layer.alert("红色显示项必填");
            return;
        }
        var data = $("#dtFrm").serialize();
        $.ajax({
            url: '/GroupPurchase/SaveGP',
            type: 'post',
            data: data + '&type=@ViewData["type"]',
            success: function (res) {
                if (res.code == 0) {
                    layer.alert(res.msg, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layui.table.reload('LAY-gp-list'); //重载表格
                        parent.layer.close(index);
                    });
                } else {
                    layer.alert(res.msg);
                }
            }
        });
    }

    function valid(fromid) {
        var not = 0;
        $(fromid).find("[verify='required']").each(function () {
            if ($(this).val() == "") {
                $(this).parent().parent().find("label").css("color", "red");
                not++;
            } else {
                $(this).parent().parent().find("label").css("color", "");
            }
        });
        return not;
    }

    function onCancel() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    function doEnabled() {
        layer.confirm('启用后将不能修改此批次的所有设置，是否确定启用？', { icon: 4}, function (index) {
            layer.close(index);
            var index2 = layer.load(1, { time: 10 * 1000});
            $.ajax({
                url: '/GroupPurchase/EnabledGP',
                type: 'post',
                data: 'gpCode=@Model.Code',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert(res.msg, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layui.table.reload('LAY-gp-list'); //重载表格
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert(res.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status + ' ' + textStatus + ' (' + errorThrown + ')');
                },
                complete: function () {
                    layer.close(index2);
                }
            });
        });
    }
</script>
